package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageResult;
import com.mall.product.dao.AttrGroupDao;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import org.springframework.stereotype.Service;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Override
    public PageResult<AttrGroupEntity> page(int current, int size, Long attrGroupId, String attrGroupName, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();

        if (attrGroupId != null) {
            queryWrapper.eq("attr_group_id", attrGroupId);
        }

        if (catelogId != null && !catelogId.equals(0L)) {
            queryWrapper.or().eq("catelog_id", catelogId);
        }

        if (attrGroupName != null && !"".equals(attrGroupName)) {
//            queryWrapper.or().like("attr_group_name", attrGroupName);
            queryWrapper.and(t -> {
                t.like("attr_group_name", attrGroupName);
            });
        }

        Page<AttrGroupEntity> page = new Page<>(current, size);
        Page<AttrGroupEntity> attrGroupEntityPage = baseMapper.selectPage(page, queryWrapper);

        return new PageResult<>(attrGroupEntityPage.getRecords(), attrGroupEntityPage.getTotal());
    }
}